package com.kaijie.admin.common.utils;





import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

 

public class linux {
	
	public static void main(String[] args) throws Exception {
		String host = "172.20.1.47";
	    int port = 22;
	    String username = "test2";
	    String password = "123456";
	    //System.out.println(linux.daily(host,username,password, "racdb", "racdb1"));;
	    String d="ps -ef|grep iostat";
        String f="iostat -c";
        //在执行ifconfig前先声明环境变量
        String g="export PATH=/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin;ifconfig | grep \"RX bytes\" | awk -F \":\" '{print $2}' | awk '{sum += $1};END {print sum}' ; sleep 1s ; /sbin/ifconfig | grep \"RX bytes\" | awk -F \":\" '{print $2}' | awk '{sum += $1};END {print sum}'";
        
        String a="export LANG=en_US;free -m|grep -yE \"total|Mem\"|sed 's/Mem:    //'|sed 's/total/total_gb/'|sed 's/used/used_gb/'|sed 's/free/free_gb/'|awk '{print $1 , $2 , $3 , $5 , $6}' ";
        List<String>sql=new ArrayList<String>();
		//sql.add(g);
		//sql.add("ifconfig | grep \"TX bytes\" | awk -F \":\" '{print $3}' | awk '{sum += $1};END {print sum}' ; sleep 1s ; ifconfig | grep \"TX bytes\" | awk -F \":\" '{print $3}' | awk '{sum += $1};END {print sum}'");
		
	            // 创建连接
	            Connection conn = new Connection(host,port);
	            // 启动连接
	            conn.connect();
	            // 验证用户密码
	            boolean rs =conn.authenticateWithPassword(username, password);
	            if (rs==false) {
	                throw new Exception("连接错误!");
	            }
	           
	            Session session = conn.openSession();		
	            session.execCommand(a);
	            //System.out.println(b);
	            //然后将返回的结果转化为输入流对象
	            InputStream stdout = new StreamGobbler(session.getStdout());
	            //然后将流对象读取出来
	            BufferedReader br = new BufferedReader(new InputStreamReader(stdout,StandardCharsets.UTF_8));
	           
	            while (true) {
	                String line=br.readLine();
	                if (line == null) {
	                    break;
	                }
	                
	                System.out.println(line);
	                
	            }
	            System.out.println(sql);
	            br.close();
	            session.close();
	    		
	            conn.close();
//
//        
//		sql.add("export PATH=/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin;ifconfig | grep \"RX bytes\" | awk -F \":\" '{print $2}' | awk '{sum += $1};END {print sum}' ; sleep 1s ; ifconfig | grep \"RX bytes\" | awk -F \":\" '{print $2}' | awk '{sum += $1};END {print sum}'");
//		sql.add("export PATH=/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin;ifconfig | grep \"TX bytes\" | awk -F \":\" '{print $3}' | awk '{sum += $1};END {print sum}' ; sleep 1s ; ifconfig | grep \"TX bytes\" | awk -F \":\" '{print $3}' | awk '{sum += $1};END {print sum}'");
//		sql=linuxosinfocollect("172.20.1.112","oracle","123456",22,sql);System.out.println(sql);
		 
	}
	public static List<String> daily(String ip,String username,String password,Integer port,String dbname,String sid) throws Exception{
		List<String>list= new ArrayList<String>();
		// 创建连接
        Connection conn = new Connection(ip,port);
        // 启动连接
        conn.connect();
        // 验证用户密码
        boolean rs =conn.authenticateWithPassword(username, password);
        if (rs==false) {
            throw new Exception("连接错误!");
        }
        Session session = conn.openSession();
        //String a="tail -n 1000 /u01/app/oracle/diag/rdbms/racdb/racdb1/trace/alert_racdb1.log|grep -E 'ORA-|Error'";
        String b=". /home/oracle/.bash_profile;echo \"set home diag/rdbms/"+dbname+"/"+sid+";SHOW ALERT -P \\\"MESSAGE_TEXT LIKE '%Error%'\\\" -TAIL 1000\"|adrci";
        session.execCommand(b);
        System.out.println(b);
        //然后将返回的结果转化为输入流对象
        InputStream stdout = new StreamGobbler(session.getStdout());
        //然后将流对象读取出来
        BufferedReader br = new BufferedReader(new InputStreamReader(stdout,StandardCharsets.UTF_8));
        int i=0;
        while (true) {
            String line=br.readLine();
            if (line == null) {
                break;
            }
            i++;
            if(i>6){
            	list.add(line);
            }
            	
        }
        br.close();
        conn.close();
        session.close();
		return list;
	}
	public static List<String> linuxrun(String ip,String username,String password,Integer port,String sql) throws Exception{
		List<String>list= new ArrayList<String>();
		// 创建连接
        Connection conn = new Connection(ip,port);
        // 启动连接
        conn.connect();
        // 验证用户密码
        boolean rs =conn.authenticateWithPassword(username, password);
        if (rs==false) {
            throw new Exception("连接错误!");
        }
        Session session = conn.openSession();       
        session.execCommand(sql);
        System.out.println(sql);
        //然后将返回的结果转化为输入流对象
        InputStream stdout = new StreamGobbler(session.getStdout());
        //然后将流对象读取出来
        BufferedReader br = new BufferedReader(new InputStreamReader(stdout,StandardCharsets.UTF_8));
        
        while (true) {
            String line=br.readLine();
            if (line == null) {
                break;
            }
            list.add(line);
        }
       
        br.close();
        conn.close();
        session.close();
		return list;
	}
	public static List<String> linuxosinfocollect(String ip,String username,String password,Integer port,List<String> sql) throws Exception{
		List<String>list= new ArrayList<String>();
		// 创建连接
        Connection conn = new Connection(ip,port);
        // 启动连接
        conn.connect();
        // 验证用户密码
        boolean rs =conn.authenticateWithPassword(username, password);
        if (rs==false) {
            throw new Exception("连接错误!");
        }
        
        for (int i = 0; i < sql.size(); i++) {
            Session session = conn.openSession();		
            session.execCommand(sql.get(i));
            //System.out.println(b);
            //然后将返回的结果转化为输入流对象
            InputStream stdout = new StreamGobbler(session.getStdout());
            //然后将流对象读取出来
            BufferedReader br = new BufferedReader(new InputStreamReader(stdout,StandardCharsets.UTF_8));
           
            while (true) {
                String line=br.readLine();
                if (line == null) {
                    break;
                }
                list.add(line);
                System.out.println(line);
                
            }
            System.out.println(sql);
            br.close();
            session.close();
    		}
       
       
        
        conn.close();
        
		return list;
	}

}




